package com.qf.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.qf.controller.BaseServlet;
import com.qf.entity.Goods;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.UUID;

/**
 * @author wgy
 * @version V1.0
 * @project java2402_xiaomi
 * @package com.qf.controller.admin
 * @company 千锋教育
 * @date 2024/6/14 10:02
 */
@WebServlet(urlPatterns = "/admin/goodsservlet")
@MultipartConfig(maxFileSize = 1024*1024*5,maxRequestSize = 1024*1024*20)
public class GoodsServlet extends BaseServlet {
    public String addGoods(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String name = request.getParameter("name");
        String typeid = request.getParameter("typeid");
        String pubdate = request.getParameter("pubdate");
        String price = request.getParameter("price");
        String star = request.getParameter("star");
        Part picture = request.getPart("picture");
        String intro = request.getParameter("intro");
        //校验
        if(StrUtil.isBlank(name)){
            return null;
        }
        if(StrUtil.isBlank(typeid)){
            return null;
        }
        if(StrUtil.isBlank(pubdate)){
            return null;
        }
        if(StrUtil.isBlank(price)){
            return null;
        }
        if(StrUtil.isBlank(star)){
            return null;
        }
        if(picture==null){
            return null;
        }
        if(StrUtil.isBlank(intro)){
            return null;
        }
        //处理图片
        String basepath = this.getServletContext().getRealPath("WEB-INF" + File.separator + "images");
        File dir=new File(basepath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        String submittedFileName = picture.getSubmittedFileName();
        String filename =null;
        if(!submittedFileName.equals("")){
            //保存文件
             filename = UUID.randomUUID().toString().replace("-", "").substring(16);
            //后缀
            int pos = submittedFileName.lastIndexOf(".");
            if(pos>=0){
                String ext = submittedFileName.substring(pos);
                filename+=ext;
            }
            picture.write(basepath+File.separator+filename);
            picture.delete();//删除缓存
        }
        //创建业务对象
        try {
            GoodsService goodsService=new GoodsServiceImpl();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Goods goods=new Goods(null,name,sdf.parse(pubdate),filename,Integer.parseInt(price),Integer.parseInt(star),intro,Integer.parseInt(typeid),null);
            goodsService.add(goods);
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
